package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.CloudUserGoodsMapper;
import com.bsh.mybatis.model.CloudUserGoods;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 用户兑换商品
 * @author gsh
 *
 */
@Repository
public class DaoCloudUserGoods {
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	
	CloudUserGoodsMapper getMapper() {
		return session.getMapper(CloudUserGoodsMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(CloudUserGoods record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(CloudUserGoods record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public CloudUserGoods selectByPrimaryKey(Integer id) {
		CloudUserGoods row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(CloudUserGoods record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(CloudUserGoods record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}
	
	/**
	 * 提现分页查询
	 */
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select ug.*,ad.login_name admin_name,u.name user_name,g.name goods_name,m.name merchant_name  ");
		sb.append("from cloud_user_goods ug "); 
		sb.append("left join cloud_goods g on g.id=ug.goods_id ");
		sb.append("left join merchant m on m.id=g.merchant_id ");
		sb.append("left join user u on u.id=ug.userid ");
		sb.append("left join admin ad on ad.id=ug.admin_id ");
		sb.append(" where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" order by id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}
	
	/**
	 * 发货
	 * @param userid
	 * @return
	 */
	public Integer audit(int merchantId, boolean isSend, String auditMsg) {
		CloudUserGoodsMapper map = this.getMapper();
		return map.sendGoods(merchantId, auditMsg, isSend?1:0);
	}
}
